import {
  getCommodity,
  addCommodity,
  deleteCommodity,
  updateCommodity,
  searchCommodity,
} from '../services/commodity';

export default {

  namespace: 'commodity',

  state: {
    commodityList: [],
  },

  subscriptions: {
      setup({ dispatch, history }) {  // eslint-disable-line
    },
  },

  effects: {
      *getCommodity({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(getCommodity, payload);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addCommodity({ payload }, { call, put }) {  // eslint-disable-line
      yield call(addCommodity, payload);
      yield put({
        type: 'getCommodity',
      });
    },
    *deleteCommodity({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(deleteCommodity, payload);
      console.log(1111, response);
      yield put({
        type: 'getCommodity',
      });
    },
    *updateCommodity({ payload }, { call, put }) {  // eslint-disable-line
      yield call(updateCommodity, payload);
      yield put({
        type: 'getCommodity',
      });
    },
    *searchCommodity({ payload }, { call, put }) {  // eslint-disable-line
      const response = yield call(searchCommodity, payload);
      const res = {
        code: response.code,
        message: response.message,
        data: [response.data],
      };
      yield put({
        type: 'save',
        payload: res,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        commodityList: action.payload.data };
    },
  },

};

